package model

import (
	"github.com/gomodule/redigo/redis"
	"strconv"
	"tizhi_front/nspel"
)

type ArticleCollect struct {
	Id        int64 `gorm:"AUTO_INCREMENT;primary_key;column:id"`
	StudentId int64 `gorm:"column:student_id"`
	ArticleID int64 `gorm:"column:article_id"`
}

func (*ArticleCollect) TableName() string {
	return "article_collect"
}

type ArticleCollectE struct{}

func (a *ArticleCollectE) SelectByStudentIdArticleId(studentId int64, articleId int64) (err error) {
	var articleCollect ArticleCollect0
	key := "article_collect0.student_id:" + strconv.FormatInt(studentId, 10) + ".article_id:" + strconv.FormatInt(articleId, 10)
	err = nspel.RedisGetGob(key, &articleCollect)
	if err == nil {
		return
	}
	if err != redis.ErrNil {
		nspel.Error(err)
	} else {
		err = nil
	}
	if err = nspel.GetOrm().Select("id").Where("student_id = ?", studentId).Where("article_id = ?", articleId).First(&articleCollect).Error; err != nil {
		return
	}
	nspel.RedisSetGob(key, articleCollect)
	return
}

func (a *ArticleCollectE) SelectAllByStudentId(studentId int64) (articleCollects []ArticleCollect2, err error) {
	key := "article_collect2.student_id:" + strconv.FormatInt(studentId, 10) + ".all"
	err = nspel.RedisGetGob(key, &articleCollects)
	if err == nil {
		return
	}
	if err != redis.ErrNil {
		nspel.Error(err)
	} else {
		err = nil
	}
	if err = nspel.GetOrm().Select("article_id").Where("student_id = ?", studentId).Find(&articleCollects).Error; err != nil {
		return
	}
	nspel.RedisSetGob(key, articleCollects)
	return
}

type ArticleCollect0 struct {
	Id int64 `gorm:"primary_key;column:id"`
}

func (*ArticleCollect0) TableName() string {
	return "article_collect"
}

func (a *ArticleCollect0) SelectByStudentIdArticleId(studentId int64, articleId int64) (err error) {
	key := "article_collect0.student_id:" + strconv.FormatInt(studentId, 10) + ".article_id:" + strconv.FormatInt(articleId, 10)
	err = nspel.RedisGetGob(key, a)
	if err == nil {
		return
	}
	if err != redis.ErrNil {
		nspel.Error(err)
	} else {
		err = nil
	}
	if err = nspel.GetOrm().Select("id").Where("student_id = ?", studentId).Where("article_id = ?", articleId).First(a).Error; err != nil {
		return
	}
	nspel.RedisSetGob(key, a)
	return
}

func (a *ArticleCollect0) Delete(studentId int64, articleId int64) (err error) {
	err = nspel.GetOrm().Delete(a).Error
	if err != nil {
		return
	}
	_ = nspel.RedisKeysDelete("article_collect0.student_id:"+strconv.FormatInt(studentId, 10)+".article_id:"+strconv.FormatInt(articleId, 10), "article_collect0.student_id:"+strconv.FormatInt(studentId, 10)+".all", "article_collect2.student_id:"+strconv.FormatInt(studentId, 10)+".all")
	return
}

type ArticleCollect1 struct {
	StudentId int64 `gorm:"column:student_id"`
	ArticleID int64 `gorm:"column:article_id"`
}

func (*ArticleCollect1) TableName() string {
	return "article_collect"
}

func (a *ArticleCollect1) Insert() (err error) {
	err = nspel.GetOrm().Create(a).Error
	_ = nspel.RedisKeysDelete("article_collect0.student_id:"+strconv.FormatInt(a.StudentId, 10)+".all", "article_collect2.student_id:"+strconv.FormatInt(a.StudentId, 10)+".all")
	return
}

type ArticleCollect2 struct {
	ArticleID int64 `gorm:"column:article_id"`
}

func (*ArticleCollect2) TableName() string {
	return "article_collect"
}
